﻿/* 清空数组 */
Array.prototype.clear = function () {
    this.length = 0;
    return this;
}

/* 添加一个项到数组里 */
Array.prototype.addItem = function (item) {
    this.push(item);
    return this;
}

/* index从0开始 */
Array.prototype.deleteItem = function (index) {
    if (index < 0 || index >= this.length) {
        return this;
    }
    else {
        return this.slice(0, index).concat(this.slice(index + 1, this.length));
    }
}

/*判断某项在数组里是否存在  true代表存在 false代表不存在*/
Array.prototype.isExist = function (val) {
    var bl = false;
    for (var i = 0; i < this.length; i++) {
        if (val == this[i]) {
            bl = true;
        }
    }
    return bl;
}

/* index从0开始,如果不存在则返回-1 */
Array.prototype.findItem = function (item) {
    var index = -1;
    if (this.length > 0) {
        for (var i = 0; i < this.length; i++) {
            if (item == this[i]) {
                index = i;
                break;
            }
        }
    }
    return index;
}

/*去除数组里的重复项 返回新的数组*/
Array.prototype.distinct = function () {
    var temp = new Array();
    this.sort();
    for (i = 0; i < this.length; i++) {
        if (this[i] == this[i + 1]) {
            continue;
        }
        temp[temp.length] = this[i];
    }
    return temp;
}

/* 数组反转 */
Array.prototype.reversed = function () {
    return this.reverse();
}

/* 数组正序排列 */
Array.prototype.ascArray = function () {
    var compare = function (x, y) {
        if (x > y) {
            return 1;
        }
        if (x < y) {
            return -1;
        }
    }
    return this.sort(compare);
}

/* 数组倒序排列 */
Array.prototype.descArray = function () {
    var compare = function (x, y) {
        if (x > y) {
            return -1;
        }
        if (x < y) {
            return 1;
        }
    }
    return this.sort(compare);
}

/* 打乱数组 */
Array.prototype.randomSort = function () {
    var compare = function (x, y) {
        return Math.random() > 0.5 ? -1 : 1;
    }
    return this.sort(compare);
}

/* 
    数据为json形式的数组,该方法支持按照某列排序.
    @column 为字段名
    @desc 传递desc为正序,不传为反序
*/
Array.prototype.sortByField = function (column, desc) {
    return this.sortBy(this, column, desc, this.sortByFn);
}
Array.prototype.sortBy = function (arr, prop, desc, fn) {
    var props = [],
    ret = [],
    i = 0,
    len = arr.length;
    if (typeof prop == 'string') {
        for (; i < len; i++) {
            var oI = arr[i];
            (props[i] = new String(oI && oI[prop] || ''))._obj = oI;
        }
    }
    else {
        throw '参数类型错误';
    }

    if (fn != null && typeof fn == 'function') {
        props.sort(fn);
    } else {
        props.sort();
    }
    for (i = 0; i < len; i++) {
        ret[i] = props[i]._obj;
    }
    if (desc) {
        ret.reverse();
    }
    return ret;
}
Array.prototype.sortByFn = function (x, y) {
    var reg = /^\d+$/g;
    var reg2 = /^\d+$/g;
    var result;
    if (reg.test(x) && reg2.test(y)) {
        var iNum1 = parseInt(x);
        var iNum2 = parseInt(y);
        if (iNum1 < iNum2) {
            result = -1;
        } else if (iNum1 > iNum2) {
            result = 1;
        } else {
            result = 0;
        }
    } else {
        result = x.localeCompare(y);
    }
    return result;
}